﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using EventAggregationQuickStart;
using Microsoft.Practices.Composite.Events;

namespace SomeService
{
    // This is an example of a background service - could be reading input on a COM port,
    // for example - which could have been defined by an interface, and other
    // modules in the system could use the services of this service by getting
    // a reference to the service through the container.
    // However, in this case, this service will publish events, and other modules
    // will subscribe to them.
    public class ServiceThatPublishesEvents : IServiceThatPublishesEvents
    {
        private Timer timer;
        private IEventAggregator eventAggregator;

        public ServiceThatPublishesEvents(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;

            // Setup a timer that fires every 10 seconds
            timer = new Timer(
                delegate(object o) 
                { 
                    System.Diagnostics.Debug.WriteLine("Timer Fired");
 
                    // Publish an event
                    SomeServiceEvent customEvent = this.eventAggregator.GetEvent<SomeServiceEvent>();
                    customEvent.Publish(
                        new DataForSomeServiceEvent()
                        {
                            SomeData = Convert.ToInt32(DateTime.Now.TimeOfDay.TotalSeconds)
                        }
                    );
                }
                , null, 0, 10000);
        }
    }
}
